package com.offcn.bigdata.dataset;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;


/**
 * @Auther: BigData-LGW
 * @ClassName: FlinkJavaDataSet
 * @Date: 2020/12/15 19:18
 * @功能描述:
 * @Version:1.0
 */
public class FlinkJavaDataSetWordCount {
    public static void main(String[] args) throws Exception {
        //获得程序的入口
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        //加载外部数据集
        DataSource<String> lines = env.fromElements(
                "love is blind",
                "where shall i go to kill some time",
                "tale is cheap, show me the code"
        );
        AggregateOperator<Tuple2<String, Integer>> ret = lines.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            public void flatMap(String line, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] words = line.split("\\s+");
                for (String word : words) {
                    out.collect(new Tuple2<String, Integer>(word, 1));
                }
            }
        })
                .groupBy(0)
                .sum(1);
        ret.print();
    }
}
class WordCount{
    private String word;
    private int count;

    public WordCount() {
    }

    public WordCount(String word, int count) {
        this.word = word;
        this.count = count;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
